package com.mmall.controller.admin;

import com.mmall.commons.Const;
import com.mmall.commons.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.ICategoryService;
import com.mmall.util.CheckUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 商品分类Controller
 * @author wgt
 * @date 2018/6/28
 * @project mmall-server
 */
@Controller
@RequestMapping("/manage/category")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    /**
     * 添加商品分类
     * @param categoryName
     * @param parentId
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add_category.do", method = RequestMethod.POST)
    public ServerResponse addCategory(String categoryName,
                                      @RequestParam(value = "parentId", defaultValue = "0")int parentId,
                                      HttpSession session){
        ServerResponse<User> checkResponse = CheckUserUtil.checkRoleAdmin(session);
        if (checkResponse.isSuccess()){
            return categoryService.addCategory(categoryName, parentId);
        }
        return checkResponse;
    }

    /**
     * 更新商品分类名称
     * @param categoryId
     * @param categoryName
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/set_category_name.do", method = RequestMethod.POST)
    public ServerResponse updateCategoryName(Integer categoryId, String categoryName,HttpSession session){
        ServerResponse<User> checkResponse = CheckUserUtil.checkRoleAdmin(session);
        if (checkResponse.isSuccess()){
            return categoryService.updateCategoryName(categoryId, categoryName);
        }
        return checkResponse;
    }

    /**
     * 获取品类子节点(平级)
     * @param categoryId
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/get_category.do", method = RequestMethod.POST)
    public ServerResponse getCategoryByParentId(@RequestParam(value = "categoryId",defaultValue = "0") int categoryId, HttpSession session){
        ServerResponse checkResponse = CheckUserUtil.checkRoleAdmin(session);
        if (checkResponse.isSuccess()){
            return categoryService.getCategoryByParentId(categoryId);
        }
        return checkResponse;
    }

    @ResponseBody
    @RequestMapping(value = "/get_deep_category.do", method = RequestMethod.POST)
    public ServerResponse getDeepCategoryByParentId(@RequestParam(value = "categoryId", defaultValue = "0") int categoryId, HttpSession session){
        ServerResponse checkResponse = CheckUserUtil.checkRoleAdmin(session);
        if (checkResponse.isSuccess()){
            return categoryService.getDeepCategoryByParentId(categoryId);
        }
        return checkResponse;
    }
}
